myApp.controller('EditJumpServerController',function($scope,$http,$stateParams){
    $scope.type = $stateParams.type;//1 新增 2 修改
    if($scope.type == '' || ($scope.type != '1' && $scope.type != '2')){
        alert("参数错误!");
        return;
    };

    $scope.runIng = false;

    $scope.server = {};
    $scope.server.ip = "";
    $scope.server.sshPort = "";
    $scope.server.level = "1";
    $scope.server.parentIp = "";
    //跳板机root密码,新增跳板机时使用
    $scope.server.sshUser = "";
    $scope.server.pass = "";
    $scope.server.rootPass = "";
    $scope.server.name = "";
    $scope.server.desc="";
    //跳板机关联工程环境列表
    $scope.selectServer = "";
    if($scope.type == '2'){
        if($stateParams.selectServer == ''){
            alert("参数错误!");
            return;
        };
        $scope.selectServer = $stateParams.selectServer;
        $http.post(java_url+'/beetle/jumpServer/getJumpServerInfo',{
            ip:$scope.selectServer,
            tokenId:getTokenId()
        }).success(function(data){
            if(data.code == '0'){
                if(data.data.count == 0){
                    return;
                }else{
                    $scope.server.ip = data.data.ip;
                    $scope.server.name = data.data.name;
                    $scope.server.desc = data.data.desc;
                    $scope.server.sshPort = data.data.sshPort;
                    $scope.server.level = data.data.level;
                    $scope.server.parentIp = data.data.parentIp;
                    return;
                };
            }else if(data.code == '2'){
                window.location.href = "/beetle/App/module/login/tpl/login.html";
                return;
            }else{
                alert(data.desc);
                return;
            };
        });
    };
    $scope.save = function(){
        if($scope.server.ip == ''){
            alert("IP不能为空！");
            return;
        };
        if($scope.server.sshPort == ''){
            alert("sshPort不能为空！");
            return;
        };

        if($scope.server.level == '2' && $scope.server.parentIp == ""){
            alert("二级跳板机的上级IP不能为空！");
            return;
        }

        $scope.runIng = true;
        $http.post(java_url+'/beetle/jumpServer/editJumpServerInfo', {
            type:$scope.type,
            ip:$scope.server.ip,
            sshPort:$scope.server.sshPort,
            level:$scope.server.level,
            parentIp:$scope.server.parentIp,
            name:$scope.server.name,
            desc:$scope.server.desc,
            tokenId:getTokenId()}
        ).success(function(data){
            $scope.runIng = false;
            if(data.code == '0'){
                alert("保存成功!");
                window.location.href = "#/menu/jumpServerList";
                return;
            }else if(data.code == '2'){
                window.location.href = "/beetle/App/module/login/tpl/login.html";
                return;
            }else{
                alert(data.desc);
                return;
            };
        });
    };
    $scope.goBack = function(){
        window.location.href = "#/menu/jumpServerList";
    };
    var isInit = false;
    $scope.initJumpServer = function () {
        if(isInit){
            return;
        };
        if($scope.server.ip == ''){
            alert("跳板机ip不能为空!");
            return;
        };
        if($scope.server.sshPort == ''){
            alert("跳板机sshPort不能为空!");
            return;
        };
        if($scope.server.sshUser == ''){
            alert("跳板机sshUser不能为空!");
            return;
        };
        if($scope.server.pass == ''){
            alert("跳板机pass不能为空!");
            return;
        };
        if($scope.server.rootPass == ''){
            alert("跳板机rootPass不能为空!");
            return;
        };
        if($scope.server.level == '2' && $scope.server.parentIp == ""){
            alert("二级跳板机的上级IP不能为空！");
            return;
        }
        document.getElementById("initJumpServer").disabled=true;
        $http.post(java_url+'/beetle/jumpServer/initJumpServer',
            {
                ip:$scope.server.ip,
                sshPort:$scope.server.sshPort,
                rootPass:$scope.server.rootPass,
                pass:$scope.server.pass,
                sshUser:$scope.server.sshUser,
                level:$scope.server.level,
                parentIp:$scope.server.parentIp,
                tokenId:getTokenId()
            }).success(function(data){
            isInit = false;
            if(data.code == '0'){
                alert("初始化成功!");
                return;
            }else if(data.code == '2'){
                window.location.href = "/beetle/App/module/login/tpl/login.html";
                return;
            }else{
                alert(data.desc);
                document.getElementById("initJumpServer").disabled=false;
                return;
            };
        });
    };
});